/*!
 * \file t___http_get_req_inf.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"

#include "../../../../src/module/http/config.h"
#include <http/comm/http_comm.h>
#include <http/http1/http_analysis.h>
#include <http/http_core.h>
#include <frct_comm.h>
#include "frreport_comm.h"
#include "../../../comm/apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

extern int __http_get_resp_inf(Frbyte_Packet *p_packet, Frreport_body_data *p_st_body_data
        , int *p_i_data_len, L7session_http_private *p_st_private
        , int i_pos_start, int i_pos_end);

static void __case1(void)
{
    FILE *fp_file = NULL;
    L7session_http_private *p_st_private = NULL;
    Frbyte_Packet *p_packet = NULL;
    Conntrack *p_st_conn = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    int i_tmp_len = 0, i_data_len = 0;
    int i_pos_start = 0;
    char *p_tmp = NULL;
    int i_ret = 0;
    int i_pos_end = 0;

    p_st_private = http_private_create();
    p_st_private->i_version = 1;
    p_st_private->p_http1_sess = http1_session_create();

    fp_file = fopen("t_http1/t_http_analysis/__http_get_resp_inf1.txt","r");
    fseek(fp_file,0,SEEK_END);
    i_tmp_len = ftell(fp_file);

    i_pos_end = i_tmp_len;

    p_tmp = (char *)frmalloc(i_tmp_len);
    rewind(fp_file);
    fread(p_tmp,i_tmp_len,1,fp_file);
    fclose(fp_file);

    p_packet = bytebuff_creat();
    bytebuff_add(p_packet, p_tmp, (unsigned int)i_tmp_len);
    frfree(p_tmp);

    apmtest_create_ct(&p_st_conn, http_session_destroy, (char *)"192.168.0.1", (char *)"192.168.0.35", 5460, 80);
    p_st_body_data = frreport_body_new_l7(p_st_conn);

    i_ret = __http_get_resp_inf(p_packet, p_st_body_data, &i_data_len, p_st_private, i_pos_start, i_tmp_len);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_EQUAL(i_data_len, 2);


    bytebuff_free(&p_packet);
    apmtest_destroy_ct(&p_st_conn);
    frreport_body_free(&p_st_body_data);
    http_session_destroy(p_st_private);

    return;
}

static void __case2(void)
{
    FILE *fp_file = NULL;
    L7session_http_private *p_st_private = NULL;
    Frbyte_Packet *p_packet = NULL;
    Conntrack *p_st_conn = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    int i_tmp_len = 0, i_data_len = 0;
    int i_pos_start = 0;
    char *p_tmp = NULL;
    int i_ret = 0;
    int i_pos_end = 0;

    p_st_private = http_private_create();
    p_st_private->i_version = 1;
    p_st_private->p_http1_sess = http1_session_create();

    fp_file = fopen("t_http1/t_http_analysis/__http_get_resp_inf2.txt","r");
    fseek(fp_file,0,SEEK_END);
    i_tmp_len = ftell(fp_file);

    i_pos_end = i_tmp_len;

    p_tmp = (char *)frmalloc(i_tmp_len);
    rewind(fp_file);
    fread(p_tmp,i_tmp_len,1,fp_file);
    fclose(fp_file);

    p_packet = bytebuff_creat();
    bytebuff_add(p_packet, p_tmp, (unsigned int)i_tmp_len);
    frfree(p_tmp);

    apmtest_create_ct(&p_st_conn, http_session_destroy, (char *)"192.168.0.1", (char *)"192.168.0.35", 5460, 80);
    p_st_body_data = frreport_body_new_l7(p_st_conn);

    i_ret = __http_get_resp_inf(p_packet, p_st_body_data, &i_data_len, p_st_private, i_pos_start, i_tmp_len);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_EQUAL(i_data_len, 151);


    bytebuff_free(&p_packet);
    apmtest_destroy_ct(&p_st_conn);
    frreport_body_free(&p_st_body_data);
    http_session_destroy(p_st_private);

    return;
}

int add_suite___http_get_resp_inf(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __http_get_resp_inf()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);


    return FR_SUCC;
}
